using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;  
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 药品管理系统
{
    public partial class frm_Record : Form
    {
        private DataTable DrugrecordTable;

        public frm_Record()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.dgv_Drugrecord.AllowUserToAddRows = false;
            this.dgv_Drugrecord.RowHeadersVisible = false;
            this.dgv_Drugrecord.BackgroundColor = Color.White;
            this.dgv_Drugrecord.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;

        }
        private void btn_Query_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
               "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                           
            SqlCommand sqlCommand = new SqlCommand();                                                       
            sqlCommand.Connection = sqlConnection;                                                          
            sqlCommand.CommandText = "SELECT * FROM tb_drugrecord;";                                             
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                         
            sqlDataAdapter.SelectCommand = sqlCommand;
            sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;                                     
           
            DataTable drugrecordTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(drugrecordTable);
            sqlConnection.Close();
            this.DrugrecordTable = drugrecordTable;                                        
            this.dgv_Drugrecord.Columns.Clear();
            this.dgv_Drugrecord.DataSource = drugrecordTable;                                                          
            this.dgv_Drugrecord.Columns["Drno"].HeaderText = "取药编号";
            this.dgv_Drugrecord.Columns["Uno"].HeaderText = "用户编号";                                
            this.dgv_Drugrecord.Columns["Mname"].HeaderText = "药品名称";
            this.dgv_Drugrecord.Columns["Dramount"].HeaderText = "取药数量";
            this.dgv_Drugrecord.Columns["Drdate"].HeaderText = "取药日期";
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
             "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                             
            SqlCommand sqlCommand = new SqlCommand();                                                       
            sqlCommand.Connection = sqlConnection;                                                         
            sqlCommand.CommandText =                                                                        
                "UPDATE tb_drugrecord"
                + " SET Uno=@Uno,Mname=@Mname,Dramount=@Dramount,Drdate=@Drdate"
                + " WHERE Drno=@Drno;";
            sqlCommand.Parameters.Add("@Uno", SqlDbType.Char , 4, "Uno");
            sqlCommand.Parameters.Add("@Drno", SqlDbType.Char, 4, "Drno");                     
            sqlCommand.Parameters.Add("@Mname", SqlDbType.VarChar , 50, "Mname");
            sqlCommand.Parameters.Add("@Dramount", SqlDbType.VarChar, 20, "Dramount");
            sqlCommand.Parameters.Add("@Drdate", SqlDbType.Date , 0, "Drdate");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                          
            sqlDataAdapter.UpdateCommand = sqlCommand;                                                      
            DataTable drugrecord = this.DrugrecordTable;                                                     
            sqlConnection.Open();                                                                          
            int rowAffected = sqlDataAdapter.Update(drugrecord );                                         
            sqlConnection.Close();                                                                         
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");                                    
        }

        private void frm_Record_Load(object sender, EventArgs e)
        {

        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                             
            sqlConnection.ConnectionString =
                "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                           
            SqlCommand deleteCommand = new SqlCommand();                                                    
            deleteCommand.Connection = sqlConnection;                                                      
            deleteCommand.CommandText =                                                                    
                "DELETE tb_drugrecord"
                + " WHERE Drno='" + textBox3.Text + "';";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                                                  
            sqlDataAdapter.DeleteCommand = deleteCommand;                                                  
            DataTable drugrecord = (DataTable)this.dgv_Drugrecord.DataSource;                                  
            sqlConnection.Open();
            int rowAffected = deleteCommand.ExecuteNonQuery();                                          
            sqlConnection.Close();                                                                        
            MessageBox.Show("删除" + rowAffected.ToString() + "行。");
            sqlConnection.Close();  

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                          
            sqlConnection.ConnectionString =
                "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                           
            SqlCommand sqlCommand = sqlConnection.CreateCommand();                                    
            sqlCommand.CommandText =
                "INSERT tb_drugrecord (Drno,Uno,Mname,Dramount,Drdate) VALUES(@Drno,@Uno,@Mname,@Dramount,@Drdate);";              
            sqlCommand.Parameters.AddWithValue("@Uno", this.textBox1 .Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Drno", this.textBox5.Text.Trim());       
            sqlCommand.Parameters.AddWithValue("@Mname", this.textBox2 .Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Dramount", this.textBox4 .Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Drdate", this.dateTimePicker1 .Text.Trim());
            
            sqlConnection.Open();                                                                       
            int rowAffected = sqlCommand.ExecuteNonQuery();                                            
            sqlConnection.Close();                                                                   
            if (rowAffected == 1)                                                                      
            {
                MessageBox.Show("添加记录成功");                                                         
            }
            else                                                                                      
            {
                MessageBox.Show("添加记录失败");                                                        
            }
        }

       

        private void 菜单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Menu a = new frm_Menu();
            a.Show();
            this.Hide();
        }

        private void 登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Login b = new frm_Login();
            b.Show();
            this.Hide();
        }

        private void 注册ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Register c = new frm_Register();
            c.Show();
            this.Hide();
        }

        private void btn_SearchByNo_Click(object sender, EventArgs e)
        {
            DataRow searchResultRow = this.DrugrecordTable.Rows.Find(this.txb_1.Text.Trim());
            DataTable searchResultTable = this.DrugrecordTable.Clone();
            searchResultTable.ImportRow(searchResultRow);
            this.dgv_Drugrecord.DataSource = searchResultTable; 
        }


    }
}
